import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const commonRoutes = [{
  path: '/login',
  name: 'login',
  meta: {
    title: '登录'
  },
  component: () => import('../components/Login.vue'),
},
{
  path: '/404',
  name: '404',
  meta: {
    title: '404'
  },
  component: () => import('../components/404.vue'),
},
{
  path: '/',
  redirect: '/home'
},
]

// 本地所有的页面 需要配合后台返回的数据生成页面
export const asyncRoutes = {
  home: {
    path: '/home',
    name: 'home',
    meta: {
      title: '系统管理'
    },
    component: () => import('../views/Home.vue'),
  },
  users: {
    path: '/users',
    name: 'users',
    meta: {
      title: '用户管理'
    },
    component: () => import('../views/users/index.vue'),
  },
  roles: {
    path: '/roles',
    name: 'roles',
    meta: {
      title: '角色管理'
    },
    component: () => import('../views/roles/index.vue'),
  },
  // products: {
  //   path: '/products',
  //   name: 'products',
  //   meta: {
  //     title: '产品管理'
  //   },
  //   component: () => import('../views/products/index.vue'),
  // },
  queryproducts: {
    path: '/queryproducts',
    name: 'queryproducts',
    meta: {
      title: '产品查询'
    },
    component: () => import('../views/products/queryProducts/index.vue'),
  },
  adminProImg: {
    path: '/adminProImg',
    name: 'adminProImg',
    meta: {
      title: '产品图片管理'
    },
    component: () => import('../views/products/adminProImg/index.vue'),
  },
  adminBanner: {
    path: '/adminBanner',
    name: 'adminBanner',
    meta: {
      title: 'banner管理'
    },
    component: () => import('../views/products/adminBanner/index.vue'),
  },
  typeproducts: {
    path: '/typeproducts',
    name: 'typeproducts',
    meta: {
      title: '产品类型'
    },
    component: () => import('../views/products/typeProducts/index.vue'),
  },
  // addproducts: {
  //     path: 'addproducts',
  //     name: 'addproducts',
  //     meta: {
  //         title: '添加产品'
  //     },
  //     component: () => import('../views/products/addProducts/index.vue'),
  // },
  carts: {
    path: '/carts',
    name: 'carts',
    meta: {
      title: '购物车管理'
    },
    component: () => import('../views/carts/index.vue'),
  },
  menus: {
    path: 'menus',
    name: 'menus',
    meta: {
      title: '菜单管理'
    },
    component: () => import('../views/menus/index.vue'),
  },
  admin_menus: {
    path: '/admin_menus',
    name: 'admin_menus',
    meta: {
      title: '添加菜单'
    },
    component: () => import('../views/menus/admin_menus.vue'),
  },
  system: {
    path: '/system',
    name: 'system',
    meta: {
      title: '添加消息'
    },
    component: () => import('../views/system/index.vue'),
  },
  admin_power: {
    path: '/admin_power',
    name: 'admin_power',
    meta: {
      title: '权限管理'
    },
    component: () => import('../views/menus/admin_power.vue'),
  },
}

const createRouter = () => new Router({
  routes: commonRoutes,
})

const router = createRouter()

export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

//挂载路由导航守卫
//每个页面都守卫，按需守卫
router.beforeEach((to, from, next) => {
  //要访问的路径
  if (to.path === '/login') return next()
  //获取token
  const tokenStr = localStorage.getItem('token')
  //如果没有携带token就直接到login页面
  if (!tokenStr) return next('/login')
  next()
})




export default router
